# -*- coding: UTF-8 -*-
import mysql.connector as db
import client.tWeibo
import random
import time
import json
import re
import sys
# 发送私信测试

def gatherFollower(weiboClient, account, follower):
	dirNo = random.randint(1, 30)
	print("开始获取 %s 的听众！ %d" % (account, dirNo))
	if follower > 600:
		loopLimit = 40
	else:
		loopLimit = (follower // 15) + 1
	fp = open('follower2\\%d\\follower_%s.json' % (dirNo, account), 'w', encoding='utf-8')
	for page in range(1, loopLimit + 1):
		txt = weiboClient.follower(account, page)
		fp.write(txt.strip() + '\n')
	fp.close()
def sliceString(words, parts):
	rtn = []
	startPos = 0
	for i in range(1, parts):
		stopPos = startPos + random.randint(1, len(words) // parts)
		rtn.append(words[startPos:stopPos])
		startPos = stopPos
	rtn.append(words[startPos:len(words)])
	return rtn

if __name__ == '__main__':
	connect = db.connect(user='root',db='collection',password='',host="127.0.0.1")
	cursor = connect.cursor()
	cursor.execute("SET SQL_MODE = 'TRADITIONAL'")
	
	sqlSelectNeedSendPrivateLitterAccount = "SELECT `account`, `nick_name`, `follower` FROM `follower` WHERE `address` LIKE '%上海%' AND `certification`='' ORDER BY `follower` DESC LIMIT 95, 3278"
	sqlSelectBakAccount = "SELECT `account`, `follower` FROM `tempaccount` WHERE `follower` > 0 ORDER BY `follower` ASC LIMIT 0, 5805"
	sqlSelectWeiboContent = "SELECT `nick`,`content`,`pic` FROM `weibo_content` WHERE `nick`='南宁肛泰中医肛肠医院'"
	
	cursor.execute(sqlSelectNeedSendPrivateLitterAccount)
	needSendPrivateLitterAccounts = cursor.fetchall()
	cursor.execute(sqlSelectBakAccount)
	bakAccounts = cursor.fetchall()
	cursor.execute(sqlSelectWeiboContent)
	weiboContents = cursor.fetchall()
	
	cursor.close()
	connect.close()
	Qfaces = ['/歪脖 ', '/走你 ', '/江南style ', '/车票 ', '/火车 ', '/年年有余 ', '/拿红包 ', '/拜年 ', '/放鞭炮 ', '/','/福字 ', '/红包 ', '/雪花 ', '/蛇年 ', '/给力 ', '/','/围观 ', '/微笑 ', '/撇嘴 ', '/','/色 ', '/发呆 ', '/得意 ', '/流泪 ', '/害羞 ', '/','/闭嘴 ', '/睡 ',  '/','/调皮 ', '/呲牙 ', '/惊讶 ', '/酷 ', '/','/偷笑 ', '/可爱 ', '/饥饿 ', '/困 ',  '/流汗 ', '/','/憨笑 ', '/大兵 ', '/奋斗 ', '/疑问 ', '/嘘 ', '/晕 ', '/再见 ',  '/抠鼻 ', '/鼓掌 ', '/糗大了 ', '/','/坏笑 ', '/左哼哼 ', '/右哼哼 ', '/哈欠 ',  '/委屈 ', '/快哭了 ', '/亲亲 ', '/可怜 ',  '/西瓜 ', '/啤酒 ', '/','/篮球 ', '/乒乓 ', '/咖啡 ', '/饭 ', '/猪头 ', '/','/玫瑰 ', '/示爱 ', '/爱心 ', '/蛋糕 ',  '/足球 ', '/瓢虫 ',  '/月亮 ', '/太阳 ', '/礼物 ', '/拥抱 ', '/','/强 ', '/握手 ', '/胜利 ', '/抱拳 ', '/勾引 ', '/拳头 ', '/爱你 ', '/NO ', '/OK ', '/爱情 ', '/','/飞吻 ', '/跳跳 ', '/怄火 ', '/转圈 ', '/磕头 ', '/','/回头 ', '/跳绳 ', '/挥手 ', '/激动 ', '/','/街舞 ', '/献吻 ', '/左太极 ', '/右太极 ', '/捶地大笑 ', '/安慰 ', '/扮鬼脸 ', '/','/加油 ', '/敬礼 ', '/潜水 ', '/伤不起 ', '/亲爱的 ', '/祈祷 ', '/蜡烛 ', '/','/给力 ', '/围观 ', '/浮云 ', '/威武 ', '/','/神马 ', '/围脖 ', '/','/雪花 ', '/手套 ', '/红包 ', '/喜糖 ', '/','/福字 ', '/鞭炮 ', '/黄丝带 ', '/黑丝带',]
	uin = 'QQ号'
	passwd = '密码'
	wb = client.tWeibo.tWeibo(uin, passwd)
	wb.login()
	sendLitters = 1
	for (account, nick_name, follower) in needSendPrivateLitterAccounts:
		wb.newMsgCount()
		print("开始向 %s 发送私信！%d" % (account, sendLitters))
		msg = ''
		for s in sliceString('便血、痔疮、肛门瘙痒、肛门不适，在线预约检查免专家挂号费！', random.randint(3, 6)):
			msg += Qfaces[random.randint(0, len(Qfaces) - 1)] + s
		# print(wb.pm_mgr(account, msg + ' http://huodong.gc16.com/zc/#wb'))
		print(wb.pm_mgr(account, msg + ' http://url.cn/9oMijE'))
		sendLitters += 1
		step = 2
		if follower > 60:
			gatherFollower(wb, account, follower)
			wb.newMsgCount()
			step = 1
		for i in range(1, step + 1):
			(bAccount, bFollower) = bakAccounts.pop()
			gatherFollower(wb, bAccount, bFollower)
			wb.newMsgCount()
		(nick, content, pic) = weiboContents.pop()
		wb.publish(content, pic)
		print("沉睡 15 秒")
		for i in range(1, 4):
			time.sleep(5)
			wb.newMsgCount()
